package com.asset.salesforce.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asset.salesforce.model.City;
import com.asset.salesforce.model.Country;
import com.asset.salesforce.model.Employee;
import com.asset.salesforce.model.Lead;
import com.asset.salesforce.model.LeadData;
import com.asset.salesforce.model.LeadDetails;
import com.asset.salesforce.model.LeadList;
import com.asset.salesforce.service.CityService;
import com.asset.salesforce.service.CountryService;
import com.asset.salesforce.service.EmployeeService;
import com.asset.salesforce.service.LeadService;

@Controller
public class LeadController {

	@Autowired
	private LeadService leadService;

	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private CityService cityService;
	@Autowired
	private CountryService countryService;
//	@RequestMapping(value="/getLeads", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
//	@ResponseBody
//	public String getAllLeads(){
//		List<Lead> leads = leadService.getLeads(1);
//		for (Lead lead : leads) {
//			if(lead.getLeadOwner()!=null)lead.setLeadOwnerId(lead.getLeadOwner().getId());
//			if(lead.getCity()!=null)lead.setCityId(lead.getCity().getId());
//			if(lead.getCountry()!=null)lead.setCountryId(lead.getCountry().getId());
//		}
//		String json=null;
//		 try {
//			 ObjectMapper objectMapper = new ObjectMapper();  
//			 objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
//			 objectMapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true);
//			 final ObjectWriter writer = objectMapper.writer().withRootName("leads");
//			 
//			json = writer.writeValueAsString(leads);
//		} catch (JsonProcessingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return json;
//	}
//	@RequestMapping(value="/getAllLeads/{empId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
//	@ResponseBody
//	public List<LeadData> getAllLeads(@PathVariable String empId){
//		List<Lead> leads = leadService.getLeads(Integer.parseInt(empId));
//		List<LeadData>leadData = new ArrayList<LeadData>();
//		for (Lead lead : leads) {
//			LeadData data = new LeadData();
//			data.setCompanyName(lead.getCompanyName());
//			data.setfName(lead.getfName());
//			Employee employee = employeeService.getById(lead.getLeadOwner().getId());
//			data.setLeadOwnerName(employee.getfName());
//			leadData.add(data);
//		}
//		return leadData;
//	}
	
	@RequestMapping(value="/getAllLeads/{empId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public LeadList getAllLeads(@PathVariable String empId){
		List<Lead> leads = leadService.getLeads(Integer.parseInt(empId));
		LeadList leadList = new LeadList();
		List<LeadData>leadData = new ArrayList<LeadData>();
		for (Lead lead : leads) {
			LeadData data = new LeadData();
			data.setId(lead.getId());
			data.setCompanyName(lead.getCompanyName());
			data.setfName(lead.getfName());
			if(lead.getLeadStatus()!=null&&!lead.getLeadStatus().equalsIgnoreCase("")){
				data.setLeadStatus(lead.getLeadStatus());
			}
			String phoneMobile = null ;
			if(lead.getMobile()!=null&&!lead.getMobile().equalsIgnoreCase("")){
				phoneMobile=lead.getMobile();
			}
			if(lead.getPhone()!=null&&!lead.getPhone().equalsIgnoreCase("")){
				phoneMobile+=", "+lead.getPhone();
			}
			if(phoneMobile!=null){
				data.setMobile(phoneMobile);
			}
			if(lead.getEmail()!=null&&!lead.getEmail().equalsIgnoreCase("")){
				data.setEmail(lead.getEmail());
			}
			Employee employee = employeeService.getById(lead.getLeadOwner().getId());
			data.setLeadOwnerName(employee.getfName()+" "+employee.getlName());
			leadData.add(data);
		}
		leadList.setLeadData(leadData);
		return leadList;
	}
	@RequestMapping(value="/getLead/{id}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public Lead getLead(@PathVariable String id){
		Lead lead = leadService.getLeadById(Integer.parseInt(id));
		Employee employee = employeeService.getById(lead.getLeadOwner().getId());
		if(employee!=null){
			lead.setLeadOwnerName(employee.getfName()+" "+employee.getlName());
		}
		if(lead.getCity()!=null){
			City city = cityService.getCityById(lead.getCity().getId());
			lead.setCityName(city.getName());
		}
		if(lead.getCountry()!=null){
			Country country = countryService.getCountryById(lead.getCountry().getId());
			lead.setCountryName(country.getCountryName());
		}
		return lead;
	}
	
	@RequestMapping(value="/addLeadGet/{empId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public LeadDetails addLeadGet(@PathVariable String empId){
		Employee employee = employeeService.getById(Integer.parseInt(empId));
		List<Country>countries = countryService.getAll();
		//List<City>cities = cityService.getAllCities();
		LeadDetails lead = new LeadDetails();
		if(employee!=null){
			lead.setOwnerName(employee.getfName()+" "+employee.getlName());
		}
		lead.setCountries(countries);
		return lead;
	}
	
	@RequestMapping(value="/addLead",method=RequestMethod.POST,consumes="application/json;charset=UTF-8")
	@ResponseBody
	public Lead addLead(@RequestBody Lead lead){
		System.out.println("sucess");
		if(lead.getLeadOwnerId()!=null){
			Employee leadOwner = employeeService.getById(lead.getLeadOwnerId());
			lead.setLeadOwner(leadOwner);
		}
		if(lead.getCountryId()!=null){
			Country country = countryService.getCountryById(lead.getCountryId());
			lead.setCountry(country);
		}
		if(lead.getCityId()!=0){
			City city = cityService.getCityById(lead.getCityId());
			lead.setCity(city);
		}
		leadService.addLead(lead);
		return lead;
	}
}
